using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

public class UIEnd : MonoBehaviour
{
    public Image Number1;
    public Image Number2;
    public Image Number3;
    public Image Number4;
    public Image Number5;
    public Image Number6;

    public List<Sprite> numbersprites = new List<Sprite>();

    public GameObject start1;
    public GameObject start2;

    public Button DownLoadButton;

    private void Awake()
    {
        DownLoadButton.onClick.AddListener(() => {
            Luna.Unity.Playable.InstallFullGame();
        });
    }
    // Start is called before the first frame update
    async void Start()
    {
        int currentScore  = 0;
        int TargetScore = BiliardGameManager.instance.CurrentScore;
        DOTween.To(() => currentScore, x => currentScore = x, TargetScore, 1f).SetEase(Ease.Linear).OnUpdate(() =>
        {
            RefreshNumberSprite1(currentScore);
        });

        await Task.Delay(2000);
        start1.gameObject.SetActive(false);
        start1.transform.DOScale(Vector3.zero,0.3f).OnComplete(() => {
            start2.gameObject.transform.localScale = Vector3.zero;
            start2.gameObject.SetActive(true);
            start2.transform.DOScale(Vector3.one,0.1f).OnComplete(() => {
                int currentScore1 = 0;
                DOTween.To(() => currentScore1, x => currentScore1 = x, TargetScore, 1f).SetEase(Ease.Linear).OnUpdate(() =>
                {
                    RefreshNumberSprite2(currentScore);
                });
            });
        });
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void RefreshNumberSprite1(int score)
    {
        int a = score / 100;
        int b = score %100/ 10;
        int c = score % 10;
        if (a == 0)
        {
            Number1.gameObject.SetActive(false);
            if (b == 0)
            {
                Number2.gameObject.SetActive(false);
            }
            else
            {
                Number2.gameObject.SetActive(true);
                Number2.sprite = numbersprites[b];
                Number2.rectTransform.sizeDelta = new Vector2(numbersprites[b].texture.width, numbersprites[b].texture.height);
                Number3.gameObject.SetActive(true);
                Number3.sprite = numbersprites[c];
                Number3.rectTransform.sizeDelta = new Vector2(numbersprites[c].texture.width, numbersprites[c].texture.height);
            }
        }
        else
        {
            Number1.gameObject.SetActive(true);
            Number1.sprite = numbersprites[a];
            Number1.rectTransform.sizeDelta = new Vector2(numbersprites[a].texture.width, numbersprites[a].texture.height);
            Number2.gameObject.SetActive(true);
            Number2.sprite = numbersprites[b];
            Number2.rectTransform.sizeDelta = new Vector2(numbersprites[b].texture.width, numbersprites[b].texture.height);
            Number3.gameObject.SetActive(true);
            Number3.sprite = numbersprites[c];
            Number3.rectTransform.sizeDelta = new Vector2(numbersprites[c].texture.width, numbersprites[c].texture.height);
        }
    
    }

    private void RefreshNumberSprite2(int score)
    {
        int a = score / 100;
        int b = score %100 / 10;
        int c = score % 10;
        if (a == 0)
        {
            Number4.gameObject.SetActive(false);
            if (b == 0)
            {
                Number5.gameObject.SetActive(false);
            }
            else
            {
                Number5.gameObject.SetActive(true);
                Number5.sprite = numbersprites[b];
                Number5.rectTransform.sizeDelta = new Vector2(numbersprites[b].texture.width, numbersprites[b].texture.height);
                Number6.gameObject.SetActive(true);
                Number6.sprite = numbersprites[c];
                Number6.rectTransform.sizeDelta = new Vector2(numbersprites[c].texture.width, numbersprites[c].texture.height);
            }
        }
        else
        {
            Number4.gameObject.SetActive(true);
            Number4.sprite = numbersprites[a];
            Number4.rectTransform.sizeDelta = new Vector2(numbersprites[a].texture.width, numbersprites[a].texture.height);
            Number5.gameObject.SetActive(true);
            Number5.sprite = numbersprites[b];
            Number5.rectTransform.sizeDelta = new Vector2(numbersprites[b].texture.width, numbersprites[b].texture.height);
            Number6.gameObject.SetActive(true);
            Number6.sprite = numbersprites[c];
            Number6.rectTransform.sizeDelta = new Vector2(numbersprites[c].texture.width, numbersprites[c].texture.height);
        }
    }
}
